package fr.ordre66.vlcs.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import fr.ordre66.vlcs.dto.Upload;
import fr.ordre66.vlcs.services.IUploadManager;


@Controller
@RequestMapping("/videos")
public class Viewer {

	@Autowired IUploadManager uploadManager;
	
    @RequestMapping(method = RequestMethod.GET)
    @Transactional(readOnly = false)
    public String simplePlayer(Model model) {
        
    	model.addAttribute("left", "viewer.jsp");
    	model.addAttribute("right", "menu.jsp");
    	
    	User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    	List<Upload> videosList = uploadManager.getAllVideos(user.getUsername());
    	
    	if(videosList == null){
    		model.addAttribute("videos", new ArrayList<Long>());
    	}
    	else{
	    	List<Long>videosIDList = new ArrayList<Long>();
	    	for(Upload u :videosList)
	    		videosIDList.add(u.getId().getId());
	    	
	    	model.addAttribute("videos", videosIDList);
    	}
    	return "template";
    }
}
